﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.WebServices;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Extension of CommBodyExt class
	/// </summary>
	public partial class CommBody
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		public Definitions.eCommBodyType CommBodyType
		{
			get { return (Definitions.eCommBodyType)this._fdCommBodyType; }
			set { this.fdCommBodyType = (int)value; }
		}

		/////////////////////////////////////////////////////////////
		// Static operations

		public static Dictionary<int, string> LoadDictionary(DebtChasedEntities ent, Definitions.eCommBodyType BodyType)
		{

			// Declare return variable
			Dictionary<int, string> Ftmp = new Dictionary<int, string>();

			// Load bodies for format
			var results = from b in ent.CommBodies
						  where (b.fdCommBodyType == (int)BodyType)
						  select new { Key = b.CommBodyId, Value = b.Name };

			// Load the dictonary
			foreach (var ob in results)
				Ftmp.Add(ob.Key, ob.Value);

			// Return variable
			return Ftmp;

		}

		public static List<CommBodySummaryTransport> LoadTransportList(DebtChasedEntities ent)
		{

			// Declare return variable
			List<CommBodySummaryTransport> Ftmp = new List<CommBodySummaryTransport>();

			// Load comm bodies
			var results = from cb in ent.CommBodies.Include("WorkflowCommBodies")
						  select new { cb.CommBodyId, cb.fdCommBodyType, cb.Name, cb.SectionList, UsageCount = cb.WorkflowCommBodies.Count };
			foreach (var result in results)
			{

				// Add to list
				Ftmp.Add(new CommBodySummaryTransport()
				{
					CommBodyId = result.CommBodyId,
					CommBodyType = result.fdCommBodyType,
					Name = result.Name,
					SectionList = result.SectionList.FromPackedCommaList(),
					UsageCount = result.UsageCount
				});

			}

			// Return variable
			return Ftmp;

		}

		public static CommBodyTransport GetCommBodyTransport(DebtChasedEntities ent, int iCommBodyId)
		{

			// Declare return variable
			CommBodyTransport Ftmp = null;

			// Load comm-bodies
			var result = (from cb in ent.CommBodies
						  where (cb.CommBodyId == iCommBodyId)
						  select new { cb }).FirstOrDefault();
			if (result != null)
			{

				// Update return variable
				Ftmp = new CommBodyTransport()
				{
					CommBodyId = result.cb.CommBodyId,
					CommBodyType = result.cb.fdCommBodyType,
					Name = result.cb.Name,
					SectionList = result.cb.SectionList.FromPackedCommaList(), 
					CommTemplateId = result.cb.CommTemplateId ?? 0, 
					Content = result.cb.Content, 
					Subject = result.cb.Subject
				};

			}

			// Return variable
			return Ftmp;

		}

		public static string SetCommBodyTransport(DebtChasedEntities ent, CommBodyTransport cbt, int iUserId)
		{

			// Declare return variable
			string strError = null;

			try
			{

				// If this is a new comm-body...
				CommBody commBody = null;
				if (cbt.CommBodyId == 0)
				{

					// Insert new comm-body
					commBody = ent.CommBodies.CreateObject();
					ent.CommBodies.AddObject(commBody);

				}
				else
				{

					// Find the comm body
					commBody = ent.CommBodies.FirstOrDefault(cb => cb.CommBodyId == cbt.CommBodyId);

				}

				// Update fields
				commBody.fdCommBodyType = cbt.CommBodyType;
				commBody.CommTemplateId = cbt.CommTemplateId == 0 ? default(int?) : cbt.CommTemplateId;
				commBody.Content = cbt.Content;
				commBody.Name = cbt.Name;
				commBody.SectionList = cbt.SectionList.ToPackedCommaList();
				commBody.Subject = cbt.Subject;

				// Create session
				UserAction.Session session = new UserAction.Session(iUserId, commBody, commBody.Name);

				// Save changes
				ent.SaveChanges();

				// Update function parameter
				cbt.CommBodyId = commBody.CommBodyId;

				// Save session
				session.SaveChanges(ent);

			}
			catch (Exception x)
			{

				// Process exception
				strError = ErrorProcessing.HandleException(x, "save comm body", iUserId);

			}

			// Return variable
			return strError;

		}

	}

}
